// var xhr = new XMLHttpRequest()
// xhr.open()
// xhr.send()
// xhr.onload = () =>{

// }
// xhr.onerror = (a) =>{
//     console.log(a);
// }

// 渲染
var b = document.querySelector('tbody')
function apply() {
    var xhr = new XMLHttpRequest()
    xhr.open('get', 'https://liu.zzgoodqc.cn/goodsx/goodslist')
    xhr.send()
    xhr.onload = () => {
        var a = JSON.parse(xhr.response).data
        // console.log(a);
        b.innerHTML = ''
        a.forEach(item => {
            b.innerHTML += ` <tr>
                            <td>${item.goods_name}</td>
                            <td>${item.stock}</td>
                            <td>${item.price}</td>
                            <td>${item.status == 1 ? '上架' : item.status == 2 ? '下架' : '其他'}</td>
                            <td>${item.sold}</td>
                            <td><img src="${item.img}" alt=""></td>
                            <td><button onclick='sc(${item.id})'>删除</button> <button onclick='gx(${item.id})'>更新</button> <button onclick='zt(${item.id},${item.status})'>更换状态</button> <button onclick='xq(${item.id})'>详情</button></td>
                        </tr>`
        })
    }
    xhr.onerror = (a) => {
        console.log(a);
    }
} apply()
// 分页
var z = document.querySelector('.bu')
// console.log(z);
var g = new XMLHttpRequest()
g.open('get', 'https://liu.zzgoodqc.cn/goodsx/goodslist')
g.send()
g.onload = () => {
    var aa = JSON.parse(g.response).data
    // console.log(aa)
    // console.log(aa.length);
    var bb = Math.ceil(aa.length / 3)
    // console.log(bb);
    for (var i = 1; i <= bb.length; i++) {
        z.innerHTML += `<button>${i}</button>`
    }
}
g.onerror = (a) => {
    console.log(a);
}

// 添加商品
function tj() {
    document.querySelector('.qq').style.display = 'block'
}
function qd() {
    var a = document.querySelectorAll('.qq input')[0].value
    var b = document.querySelectorAll('.qq input')[1].value
    var c = document.querySelectorAll('.qq input')[2].value
    var d = document.querySelectorAll('.qq input')[3].value
    var e = document.querySelectorAll('.qq input')[4].value
    var f = document.querySelectorAll('.qq input')[5].value
    var xhr = new XMLHttpRequest()
    xhr.open('post', 'https://liu.zzgoodqc.cn/goodsx/add')
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
    var arr = {
        goods_name: a,
        stock: b,
        price: c,
        status: d,
        sold: e,
        img: f
    }
    xhr.send(JSON.stringify(arr))
    xhr.onload = () => {
        apply()
        document.querySelector('.qq').style.display = 'none'
    }
    xhr.onerror = (a) => {
        console.log(a);
    }
}
function sc(iid) {
    var xhr = new XMLHttpRequest()
    xhr.open('get', 'https://liu.zzgoodqc.cn/goodsx/delete?id=' + iid)
    xhr.send()
    xhr.onload = () => {
        apply()
    }
    xhr.onerror = (a) => {
        console.log(a);
    }
}
// 更新商品
var iid
function gx(id) {
    iid = id
    document.querySelector('.rr').style.display = 'block'
    // console.log(id);    
}
function qdd() {
    var a = document.querySelectorAll('.rr input')[0].value
    var b = document.querySelectorAll('.rr input')[1].value
    var c = document.querySelectorAll('.rr input')[2].value
    var d = document.querySelectorAll('.rr input')[3].value
    var e = document.querySelectorAll('.rr input')[4].value
    var f = document.querySelectorAll('.rr input')[5].value
    var xhr = new XMLHttpRequest()
    xhr.open('post', 'https://liu.zzgoodqc.cn/goodsx/update?id=' + iid)
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
    // console.log(iid);
    var arr = {
        goods_name: a,
        stock: b,
        price: c,
        status: d,
        sold: e,
        img: f
    }
    // console.log(arr);
    xhr.send(JSON.stringify(arr))
    xhr.onload = () => {
        apply()
        document.querySelector('.rr').style.display = 'none'
    }
    xhr.onerror = (a) => {
        console.log(a);
    }
}
// 搜索
function ss() {
    document.querySelector('.tt').style.display = 'block'
}
function qddd() {
    var a = document.querySelector('.tt input')
    // console.log(a.value);
    var xhr = new XMLHttpRequest()
    xhr.open('post', 'https://liu.zzgoodqc.cn/goodsx/search?goods_name=' + a.value)
    xhr.send()
    xhr.onload = () => {
        var c = JSON.parse(xhr.response).data[0]
        // console.log(c);
        b.innerHTML = ` <tr>
        <td>${c.goods_name}</td>
        <td>${c.stock}</td>
        <td>${c.price}</td>
        <td>${c.status == 1 ? '上架' : c.status == 2 ? '下架' : '其他'}</td>
        <td>${c.sold}</td>
        <td><img src="${c.img}" alt=""></td>
        <td><button onclick='sc(${c.id})'>删除</button> <button onclick='gx(${c.id})'>更新</button>  <button onclick='zt(${c.id},${c.status})'>更换状态</button></td>
    </tr>`
        document.querySelector('.tt').style.display = 'none'

    }
    xhr.onerror = (a) => {
        console.log(a);
    }
}
var oo = document.querySelector('.oo')
// console.log(oo);
oo.addEventListener('click', () => {
    console.log(111);
    location.assign('./柱状图列表.html')
})
// 排序
function fp(x, c) {
    var xhr = new XMLHttpRequest()
    xhr.open('get', `https://liu.zzgoodqc.cn/goodsx/sort?sort=${x}&type=${c}&page=1&limit=10`)
    xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    xhr.send()
    xhr.responseType = 'json'
    xhr.onload = () => {
        b.innerHTML = ''
        var a = xhr.response.data
        a.forEach(item => {
            b.innerHTML += `
        <tr>
            <td>${item.goods_name}</td>
            <td>${item.stock}</td>
            <td>${item.price}</td>
           <td>${item.status == 1 ? '上架' : item.status == 2 ? '下架' : '其他'}</td>
            <td>${item.sold}</td>
            <td><img src="${item.img}" alt=""></td>
            <td><button onclick='sc(${item.id})'>删除</button> <button onclick='gx(${item.id})'>更新</button>  <button onclick='zt(${item.id},${item.status})'>更换状态</button></td>
        </tr>`
            // console.log(x);
            // console.log(c);
        })
    }
    xhr.onerror = (e) => {
        console.log(e);
    }
}
function px() {
    // console.log(11);
    // 价格
    var a = document.querySelector('.pl').value
    var b = document.querySelector('.lp').value
    // console.log(a,b);
    // console.log(fn());

    if (a == '价格' && b == '从大到小') {
        fp('desc', 'price')
    } else if (a == '价格' && b == '从小到大') {
        fp('asc', 'price')
    }
    if (a == '库存' && b == '从大到小') {
        fp('desc', 'stock')
    } else if (a == '库存' && b == '从小到大') {
        fp('asc', 'stock')
    }
    if (a == '已售' && b == '从大到小') {
        fp('desc', 'sold')
    } else if (a == '已售' && b == '从小到大') {
        fp('asc', 'sold')
    }
}
// 状态
function zt(tt, yy) {
    if (yy == 1 && '上架') {
        yy = 2
    } else if (yy == 2 && '下架') {
        yy = 1
    }
    var xhr = new XMLHttpRequest()
    xhr.open('get', `https://liu.zzgoodqc.cn/goodsx/updateStatus?id=${tt}&status=${yy}`)
    xhr.send()
    xhr.responseType = 'json'
    xhr.onload = () => {
        apply()
        // console.log(tt, yy);
    }
    xhr.onerror = (e) => {
        console.log(e);
    }
}
// 详情页
function xq(iid) {
    location.assign('./详情页.html?id=' + iid)
    // console.log(iid)
}